﻿namespace H2.Streams.Sections
{
    using H2.Streams;
    using System;

    public class SbspInfo
    {
        internal H2.Streams.Sections.LightMap LightMapCache;
        private MapStream Map;
        internal int StartOffset;
        private string tagPathCache;

        internal SbspInfo(MapStream map, int startOffset, int index)
        {
            this.Map = map;
            this.StartOffset = startOffset;
            this.tagPathCache = this.Map.Tags.GetTagPath(this.ID, Tags.SearchType.ID);
            this.LightMapCache = new H2.Streams.Sections.LightMap(map, index);
        }

        public int ID
        {
            get
            {
                return this.Map.ReadInt32At(this.StartOffset + 20, false);
            }
            set
            {
                this.Map.WriteAt(this.StartOffset + 20, value, false);
            }
        }

        public H2.Streams.Sections.LightMap LightMap
        {
            get
            {
                return this.LightMapCache;
            }
            set
            {
                this.LightMapCache = value;
            }
        }

        public int LightMapID
        {
            get
            {
                return this.Map.ReadInt32At(this.StartOffset + 0x1c, false);
            }
            set
            {
                this.Map.WriteAt(this.StartOffset + 0x1c, value, false);
            }
        }

        public int Magic
        {
            get
            {
                return (this.Map.ReadInt32At(this.StartOffset + 8, false) - this.Offset);
            }
            set
            {
                this.Map.WriteAt(this.StartOffset + 8, (int) (value + this.Offset), false);
            }
        }

        public int Offset
        {
            get
            {
                return (this.Map.ReadInt32At(this.StartOffset, false) & 0x3fffffff);
            }
            set
            {
                this.Map.WriteAt(this.StartOffset, value, false);
            }
        }

        public int PointerOffset
        {
            get
            {
                return (this.StartOffset - this.Map.Scnr.Offset);
            }
        }

        public int Size
        {
            get
            {
                return this.Map.ReadInt32At(this.StartOffset + 4, false);
            }
            set
            {
                this.Map.WriteAt(this.StartOffset + 4, value, false);
            }
        }

        public string TagPath
        {
            get
            {
                return this.tagPathCache;
            }
            set
            {
                this.tagPathCache = value;
                this.Map.Tags.Rename(this.ID, Tags.SearchType.ID, value);
            }
        }
    }
}

